"""
存储异常模块
定义存储相关的自定义异常
"""

class StorageException(Exception):
    """存储异常基类"""
    
    def __init__(self, message: str, code: str = None, details: dict = None):
        self.message = message
        self.code = code or 'STORAGE_ERROR'
        self.details = details or {}
        super().__init__(self.message)

class StorageError(StorageException):
    """通用存储错误"""
    
    def __init__(self, message: str = "存储操作失败", details: dict = None):
        super().__init__(message, 'STORAGE_ERROR', details)

class FileNotFoundError(StorageException):
    """文件未找到错误"""
    
    def __init__(self, message: str = "文件不存在", details: dict = None):
        super().__init__(message, 'FILE_NOT_FOUND', details)

class FileTooLargeError(StorageException):
    """文件过大错误"""
    
    def __init__(self, message: str = "文件大小超过限制", details: dict = None):
        super().__init__(message, 'FILE_TOO_LARGE', details)

class InvalidFileTypeError(StorageException):
    """无效文件类型错误"""
    
    def __init__(self, message: str = "文件类型不被允许", details: dict = None):
        super().__init__(message, 'INVALID_FILE_TYPE', details)

class StorageQuotaExceededError(StorageException):
    """存储配额超出错误"""
    
    def __init__(self, message: str = "存储配额已满", details: dict = None):
        super().__init__(message, 'STORAGE_QUOTA_EXCEEDED', details)

class StorageConnectionError(StorageException):
    """存储连接错误"""
    
    def __init__(self, message: str = "存储连接失败", details: dict = None):
        super().__init__(message, 'STORAGE_CONNECTION_ERROR', details)

class StoragePermissionError(StorageException):
    """存储权限错误"""
    
    def __init__(self, message: str = "存储权限不足", details: dict = None):
        super().__init__(message, 'STORAGE_PERMISSION_ERROR', details)

class StorageConfigurationError(StorageException):
    """存储配置错误"""
    
    def __init__(self, message: str = "存储配置错误", details: dict = None):
        super().__init__(message, 'STORAGE_CONFIGURATION_ERROR', details)

class StorageMigrationError(StorageException):
    """存储迁移错误"""
    
    def __init__(self, message: str = "存储迁移失败", details: dict = None):
        super().__init__(message, 'STORAGE_MIGRATION_ERROR', details)

def handle_storage_exception(error: StorageException):
    """
    处理存储异常的统一函数
    
    Args:
        error: 存储异常
        
    Returns:
        tuple: (响应字典, HTTP状态码)
    """
    from flask import jsonify
    
    response = {
        'error': error.code,
        'message': error.message,
        'success': False
    }
    
    if error.details:
        response['details'] = error.details
    
    # 根据异常类型确定状态码
    status_code = 500  # 默认状态码
    
    if isinstance(error, FileNotFoundError):
        status_code = 404
    elif isinstance(error, (FileTooLargeError, InvalidFileTypeError, StorageQuotaExceededError)):
        status_code = 400
    elif isinstance(error, StoragePermissionError):
        status_code = 403
    elif isinstance(error, StorageConnectionError):
        status_code = 503  # 服务不可用
    
    return jsonify(response), status_code